﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChartSharp
{
    public class DataSets : ChartParam
    {
        protected IEncodingProcessor _processor;

        public override string Name
        {
            get { return "chd"; }
        }

        public override string Data
        {
            get { return _processor.Generate(); }
        }

        public DataSets(IEnumerable<IEnumerable<int>> dataSets)
        {
            if (IsSimpleEncoderEnough(dataSets))
            {
                _processor = new EncodingProcessor<SimpleEncoder, int>(dataSets);
            }
            else
            {
                _processor = new EncodingProcessor<ExtendedEncoder, int>(dataSets);
            }
        }

        public DataSets(IEnumerable<int> dataSet)
        {
            if (IsSimpleEncoderEnough(dataSet))
            {
                _processor = new EncodingProcessor<SimpleEncoder, int>(dataSet);
            }
            else
            {
                _processor = new EncodingProcessor<ExtendedEncoder, int>(dataSet);
            }
        }

        public DataSets(IEnumerable<IEnumerable<float>> dataSets)
        {
            _processor = new EncodingProcessor<TextEncoder, float>(dataSets);
        }

        public DataSets(IEnumerable<float> dataSet)
        {
            _processor = new EncodingProcessor<TextEncoder, float>(dataSet);
        }

        private static bool IsSimpleEncoderEnough(IEnumerable<int> dataSet)
        {
            bool allow = true;

            SimpleEncoder encoder = new SimpleEncoder();

            foreach (int i in dataSet)
            {
                if (i > encoder.MaxValidValue)
                {
                    allow = false;
                    break;
                }
            }

            return allow;
        }

        private static bool IsSimpleEncoderEnough(IEnumerable<IEnumerable<int>> dataSets)
        {
            bool allow = true;

            foreach (IEnumerable<int> set in dataSets)
            {
                if (!IsSimpleEncoderEnough(set))
                {
                    allow = false;
                    break;
                }
            }

            return allow;
        }
    }
}
